package com.zy.project.system.login;


import com.zy.core.util.servlet.ServletUtils;
import com.zy.core.web.BaseController;
import com.zy.core.web.entity.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录,登出的conroller
 * Package: core.controller
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-9-7 21:45
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
@Controller
@Api(description = "系统用户登录登出接口")
public class LoginConroller extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(LoginConroller.class);

    @GetMapping("/login")
    @ApiIgnore
    public String login(HttpServletRequest request, HttpServletResponse response) {
        // 如果是Ajax请求，返回Json字符串。
        if (ServletUtils.isAjaxRequest(request)) {
            return ServletUtils.renderJson(response, new JsonResult().failure("未登录或登录超时。请重新登录").toJsonString());
        }
        return "login";
    }

    /**
     * 执行ajax登录,成功后前端重定向到主页面
     * @param request
     * @param response
     * @param username 登录名
     * @param password 登录密码
     * @param rememberMe 记住我
     * @return
     */
    @ApiOperation(value = "用户登录", notes = "ajax登录成功后应该重定向到index")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "username",value = "登录名称", required = true, dataType = "String"),
        @ApiImplicitParam(name = "password",value = "登录密码", required = true, dataType = "String"),
        @ApiImplicitParam(name = "rememberMe",value = "记住我", required = false, dataType = "Boolean")
    })
    @PostMapping("/login")
    @ResponseBody
    public JsonResult login(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(name = "username",required = true) String username,
                            @RequestParam(name = "password",required = true) String password,
                            @RequestParam(name = "rememberMe",required = false) Boolean rememberMe){
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        JsonResult result = null;
        try {
            subject.login(token);
            result = success();
            log.info("用户[" + username + "]登录了系统");
        }catch (AuthenticationException e) {
            log.error(e.getMessage(),e);
            result = error("用户或密码错误");
        }finally {
            return result;
        }
    }

    @GetMapping("/index")
    @ApiIgnore
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap model){
        return "index";
    }

    @GetMapping("/main")
    @ApiIgnore
    public String main(HttpServletRequest request, HttpServletResponse response, ModelMap model){
        return "main";
    }


    @GetMapping("/test")
    @ApiIgnore
    @RequiresPermissions("user:add")
    public String permissions(HttpServletRequest request, HttpServletResponse response){
        System.out.println("新增权限!!!!!!!!!!!!");
        return null;
    }

    @GetMapping("/testUpdate")
    @ApiIgnore
    @RequiresPermissions("user:update")
    public String update(HttpServletRequest request, HttpServletResponse response){
        System.out.println("修改权限!!!!!!!!!!!!");
        return null;
    }
}
